package cn.b.sky.form.form.builder.impl;

import cn.b.sky.form.form.builder.BaseElementBuilder;
import cn.b.sky.form.model.BusinessFieldTypeDef;
import cn.b.sky.form.model.ScreenField;
import cn.b.sky.form.utils.BusinessTypeDefinitionUtils;
import org.apache.commons.lang3.ClassUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * <strong>Title : ElementBuilder.java<br></strong>
 * <strong>Package : cn.b.sky.form.form.builder.impl<br></strong>
 * <strong>Description : 元素构建器中转站</strong><br> 
 * <strong>Create on : 2016-11-9 下午12:21:42<br></strong>
 * <p>
 * @author lidapeng<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人|修改日期|修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
public class ElementBuilder {

  public static final Logger logger = LoggerFactory.getLogger(ElementBuilder.class);

  private static ElementBuilder elementBuilder;

  private final Map<String, BaseElementBuilder> builders;

  private ElementBuilder() {

    builders = new HashMap<String, BaseElementBuilder>();

    List<BusinessFieldTypeDef> fieldTypes = BusinessTypeDefinitionUtils.getFieldTypeList();
    try {
      for (BusinessFieldTypeDef fieldType : fieldTypes) {
        if (fieldType != null && !StringUtils.isBlank(fieldType.getBuilder())) {
          try {
            Class<?> builder = ClassUtils.getClass(BaseElementBuilder.class.getClassLoader(), fieldType.getBuilder());
            if (builder != null) {
              if (builder.equals(BaseElementBuilder.class)) {
                Object obj = builder.newInstance();
                builders.put(fieldType.getId(), (BaseElementBuilder) obj);
              } else {
                if (builder.getSuperclass().equals(BaseElementBuilder.class)) {
                  Object obj = builder.newInstance();
                  builders.put(fieldType.getId(), (BaseElementBuilder) obj);
                }
              }
            }
          } catch (ClassNotFoundException e) {
            logger.error("没有找到类" + fieldType.getBuilder(), e);
          } catch (Exception e) {
            logger.error("{}", e);
          }
        }
      }
    } catch (Exception e) {
      logger.error("{}", e);
    }
  }

  public static void main(String[] args) {
    getElementBuilder();
  }

  public static ElementBuilder getElementBuilder() {
    if (elementBuilder == null) {
      elementBuilder = new ElementBuilder();
    }
    return elementBuilder;
  }

  public String buildHTMLElement(ScreenField field) {
    if (field == null || field.getBf() == null) {
      return "";
    }
    BaseElementBuilder builder = builders.get(field.getBf().getFieldType().toUpperCase());
    if (builder != null) {
      return builder.toHtmlElement(field);
    } else {
      logger.warn("没有找到类型为" + field.getBf().getFieldType() + "的建造器");
      return "not found element";
    }
  }
}
